<?
include('adodb/adodb-exceptions.inc.php'); 
require ("comun/ini.php");
if(isset($_POST['JsonEnv'])){
	$JsonRec = new Services_JSON();
	$JsonEnv = new Services_JSON();
	$JsonRec=$JsonRec->decode(str_replace("\\","",$_POST['JsonEnv']));
	try {
		$q = "SELECT SUM((A.hasta - A.desde) + 1) AS cantidad 
			FROM asignacion_material_detalle AS A
				INNER JOIN asignacion_material AS B ON A.id_asignacion = B.id
			WHERE B.id_perito = $JsonRec->Perito AND A.id_tipo = $JsonRec->TipoMaterial";
		$r = $conn->Execute($q);
		$i=0;
		
		
		if (!$r->EOF) {
			$CantidadTotal = $r->fields['cantidad'];
		} else {
				$CantidadTotal = 0;
		}
		
		$q2 = "SELECT COUNT(*) AS cantidad
			FROM material_anulado AS A
			WHERE A.id_perito = $JsonRec->Perito AND A.id_tipo = $JsonRec->TipoMaterial";
		
		$r2= $conn->Execute($q2);
		
		if (!$r2->EOF) {
			$CantidadAnulado = $r2->fields['cantidad'];
		} else {
			$CantidadAnulado = 0;
		}

		$q3 = "SELECT COUNT(*) AS cantidad 
			FROM experticias AS A 
			WHERE A.hoja  IS NOT NULL AND A.id_perito = $JsonRec->Perito AND A.id_tipo = $JsonRec->TipoMaterial";
		
		$r3= $conn->Execute($q3);
		
		if (!$r3->EOF) {
			$CantidadUsado = $r3->fields['cantidad'];
		} else {
			$CantidadUsado = 0;
		}
		
		//die ("Cantidad " . $JsonRec->Cantidad . " - Total " . $CantidadTotal . " - Anulado " . $CantidadAnulado . " - Usado " . $CantidadUsado);
		if ($JsonRec->Cantidad <= ($CantidadTotal - $CantidadAnulado - $CantidadUsado)) {
			$Verifica[0]['V'] = 'S';
		} else {
			$Verifica[0]['V'] = 'N';
		}
		
		if(is_array($Verifica)){
			echo $JsonEnv->encode($Verifica);
		}else{
			echo false;
		}
	}catch( ADODB_Exception $e ){
		//	echo ERROR_CATCH_GENERICO;
		echo false;
	}
} 
?>